package com.lanyou.esb.cook.proxy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lanyou.esb.cook.proxy.meta.Environment;

/**
 * 服务测试代理接口
 * 
 * @author Davey.wu
 */
public class ServiceTestHttpProxyServlet extends HttpProxyServlet {

	/****/
	private static final long serialVersionUID = -6392587153334176366L;

	/** HTTP请求代理 **/
	private Proxy proxy;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 系统编码
		String systemCode = null;
		// 接口版本
		String ifVersion = null;
		// 接口编码
		String ifCode = null;
		try {
			/**
			 * 测试接口请求地址的统一规范格式:
			 * http://{domain}/test/{systemcode}/{version}/{interface_code
			 * }/{query_string}
			 ***/
			String[] patterns = request.getRequestURI().substring(1).split("/");
			systemCode = patterns[1];
			ifVersion = patterns[2];
			ifCode = patterns[3];
		} catch (Exception e) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST);
			return;
		}
		proxy.process(request, response, this, systemCode, ifCode, ifVersion,
				Environment.TEST);
	}

	public Proxy getProxy() {
		return proxy;
	}

	public void setProxy(Proxy proxy) {
		this.proxy = proxy;
	}
}
